package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookManageMapper;
import com.kgc.novel.dao.BookTagMapper;
import com.kgc.novel.domain.entity.BookManage;
import com.kgc.novel.domain.entity.BookTag;
import com.kgc.novel.domain.param.BookManageParam;
import com.kgc.novel.domain.vo.BookManageVO;
import com.kgc.novel.service.book.BookManageService;
import com.kgc.novel.util.CutOut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookManageServiceImpl extends ServiceImpl<BookManageMapper, BookManage> implements BookManageService {

    @Resource
    private BookManageMapper bookManageMapper;

    @Resource
    private BookTagMapper bookTagMapper;


    @Override
    public Res selectBookManageAll(BookManageParam bookManageParam) {

        List<BookManageVO> allManage = bookManageMapper.getAllManage(bookManageParam);
        Long allManageCount = bookManageMapper.getAllManageCount(bookManageParam.getBookName(), bookManageParam.getAuthorId(), bookManageParam.getCode(), bookManageParam.getClassifyId(), bookManageParam.getTagId(), bookManageParam.getEnd(),bookManageParam.getStatus());

        for (BookManageVO bookManageVO : allManage) {
            if (bookManageVO.getTagId()!=null){
                bookManageVO.setTagIds(CutOut.tagId(bookManageVO.getTagId()));
            }
        }

        LambdaQueryWrapper<BookTag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        List<BookTag> bookTags = bookTagMapper.selectList(lambdaQueryWrapper);

        List<String> tagName=new ArrayList<>();

        for (BookManageVO bookManageVO : allManage) {
            for (String tagId : bookManageVO.getTagIds()) {
                for (BookTag bookTag : bookTags) {
                    if (tagId.equals(bookTag.getId())){
                        tagName.add(bookTag.getTagName());
                        bookManageVO.setTagNames(tagName);
                    }
                }
            }
            tagName=new ArrayList<>();
        }

        return Res.returnData(allManage,allManageCount);
    }

    @Override
    public Res selectBookManageById(String id) {
        BookManageVO manageById = bookManageMapper.getManageById(id);
        if(manageById==null){
            return Res.returnErrMsg("没有找到该小说!");
        }
        manageById.setTagIds(CutOut.tagId(manageById.getTagId()));

        LambdaQueryWrapper<BookTag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        List<BookTag> bookTags = bookTagMapper.selectList(lambdaQueryWrapper);

        List<String> tagName=new ArrayList<>();
        for (String tagId : manageById.getTagIds()) {
            for (BookTag bookTag : bookTags) {
                if (tagId.equals(bookTag.getId())){
                    tagName.add(bookTag.getTagName());
                    manageById.setTagNames(tagName);
                }
            }
        }

        return Res.returnData(manageById);
    }

    @Override
    public Res deleteById(String id) {
        BookManage bookManage=new BookManage();
        bookManage.setId(id);
        bookManage.setDeleted((byte)1);
        bookManageMapper.updateById(bookManage);
        return Res.returnData("删除成功");
    }

    @Override
    public Res updateStatus0(String id) {
        BookManage bookManage=new BookManage();
        bookManage.setId(id);
        bookManage.setStatus((byte)0);
        bookManageMapper.updateById(bookManage);
        return Res.returnData("上架成功");
    }

    @Override
    public Res updateStatus3(String id) {
        BookManage bookManage=new BookManage();
        bookManage.setId(id);
        bookManage.setStatus((byte)3);
        bookManageMapper.updateById(bookManage);
        return Res.returnData("驳回审核成功");
    }

    @Override
    public Res updateStatus1(String id) {
        BookManage bookManage=new BookManage();
        bookManage.setId(id);
        bookManage.setStatus((byte)1);
        bookManageMapper.updateById(bookManage);
        return Res.returnData("下架成功");
    }
}
